package com.opensource.lzx.restartclient.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 739891618@qq.com
 * @Date: 2022-11-13 12:28:30
 * @ClassName: SpringBeanUtils
 * @Describe: this is a ...
 */
@Service
public class SpringBeanUtils implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static<T> List<T> getBeanOfType(Class<T> clazz) {
        return new ArrayList<T>(){{addAll(context.getBeansOfType(clazz).values());}};
    }

    public static<T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }

    public static<T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }
}
